angular.module('wf-utils')
.filter('constantTransfer',()->
    ##配合commonConstant中value-name的格式,获取目标key对应的value。
    ##例1：   data|constantTransfer:list，key默认取value，value默认取name
    ##例2：   data|constantTransfer:list,keyCode,valueCode
    return (code, list,key,value)->
        key=key||'value'
        value=value||'name'
        tmp = _.find(list,(item)->item[key]+'' == code+'')
        if (tmp)
            return tmp[value]
        else
            return code
)

.filter('binConstantTransfer',()->
    return (code, theList,key,value)->
        if code==undefined
            return ''
        key=key||'value'
        value=value||'name'
        list=angular.copy(theList)
        list=_.filter(list,(item)->item[key]!=undefined)
        binVal=code.toString(2)
        len=binVal.length
        n = list.length
        if len>n
            binVal=binVal.substr(binVal-n,len)
        else
            while len < n
                binVal = "0" + binVal
                len++
        binList=binVal.split('').reverse()
        nameList=[]
        for val,i in binList
            if binList[i]=='1'
                nameList.push(list[i].name)
        return nameList.join('/')
)

.filter('dateTransfer',()->
    return (d, format)->
        if (d == undefined)
            return d;
        date = new Date(d);
        if (date.getTime() > 0)
            return moment(date).format(format)
        else
            return d;
)
.filter('objHasChecked',()->
    return (obj)->
        _.each obj,(val,key)->
            if val==true
                return true
        return false
)
.filter('amountTransfer',()->
    return (obj)->
        if obj!=undefined and obj==parseFloat(obj)
            num=Math.floor(obj*100)/100
            return num.toFixed(2)
        else
            obj
)
.filter('cnNumTransfer',()->
    return (obj)->
        cnNumList=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五','十六','十七','十八','十九','二十']
        if obj in [1..20]
            return cnNumList[obj-1]
        else
            return obj
)
.filter('increaseTransfer',()->
    return (cur, pre)->
        if !isNaN(cur) and !isNaN(pre) and pre!=0!=cur
            return (Math.round(((cur-pre)/pre) * 10000)/100).toFixed(2) + '%';
        else
            return '--';
)
.filter('absFilter',()->
    return (obj)->
        if !isNaN(obj)
            Math.abs(obj)
        else
            obj
)
